package com.tongtailian.www.stir.base;

import android.androidlib.base.IBaseActivity;
import android.androidlib.utils.rxutil.RxBus;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tongtailian.www.stir.R;
import com.tongtailian.www.stir.utils.AppManager;
import com.tongtailian.www.stir.utils.SPManager;

import androidx.annotation.RequiresApi;

import butterknife.ButterKnife;

/**
 * @author liuml
 * @explain
 * @time 2018/10/15 11:12
 */
public abstract class BaseActivity extends IBaseActivity {
    private RelativeLayout baseRelativeLayout;
    private RelativeLayout rlTitle;
    public LinearLayout llLeft;
    private TextView tvLeft;
    private ImageView ivLeft;
    private TextView titleName;
    public LinearLayout llRight;
    private TextView tvLeftright;
    public TextView tvRight;
    private RelativeLayout rlSearch;
    private TextView txtCancel;
    public SPManager spManager;
    protected Activity mAct;
    private static final String TAG = "BaseActivity";
    private ImageView ivMore;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    public void onCreate(Bundle savedInstanceState) { spManager = SPManager.instance(this);
        RxBus.get().register(this);
        super.onCreate(savedInstanceState);
        mAct = this;
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
//        initBaseTitle();
    }

    public void initBaseTitle() {
        //默认白色
//        setStatusBar();

        baseRelativeLayout = findViewById(R.id.base_relative_layout);
        rlTitle = findViewById(R.id.rl_title);
        llLeft = findViewById(R.id.ll_left);
        tvLeft = findViewById(R.id.tv_left);
        ivLeft = findViewById(R.id.iv_left);
        titleName = findViewById(R.id.title_name);
        llRight = findViewById(R.id.ll_right);
        tvLeftright = findViewById(R.id.tv_leftright);
        tvRight = findViewById(R.id.tv_right);
//        rlSearch = findViewById(R.id.rl_search);
        ivMore = (ImageView) findViewById(R.id.iv_more);

        if (llLeft != null) {
            llLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }


    }

    public void setTvRight(String right) {
        tvRight.setText(right);
        tvRight.setVisibility(View.VISIBLE);

    }
    public void setIvMore() {
        ivMore.setVisibility(View.VISIBLE);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    public Activity getActivity() {
        return mAct;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setStatusTextColorNormal() {
        if (getActivity() != null && getActivity().getWindow() != null) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }
    }
//
//    protected void setStatusBar() {
////        StatusBarUtil.setDarkMode(this);
//        int color = getResources().getColor(R.color.white);
////        StatusBarUtil.setColor(this, getResources().getColor(R.color.bg_basetitle),0);
//        StatusBarUtil.setColor(this, color, 0);
//        StatusBarUtil.setLightMode(this);
//    }
//
//    protected void setStatusBar(int color) {
//        StatusBarUtil.setColor(this, getResources().getColor(color), 112);
//    }

    public void setTitle(String title) {
        initBaseTitle();
        if (titleName != null) {
            titleName.setText(title);
        }
    }

    /**
     * 设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值
     */
    @SuppressLint("NewApi")
    public static void setColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 生成一个状态栏大小的矩形
            View statusView = createStatusView(activity, color);
            // 添加 statusView 到布局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(statusView);
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

    /**
     * 生成一个和状态栏大小相同的矩形条 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @return 状态栏矩形条
     */
    private static View createStatusView(Activity activity, int color) {
        // 获得状态栏高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);

        // 绘制一个和状态栏一样高的矩形
        View statusView = new View(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                statusBarHeight);
        statusView.setLayoutParams(params);
        statusView.setBackgroundColor(color);
        return statusView;
    }

    /**
     * 使状态栏透明 * <p> * 适用于图片作为背景的界面,此时需要图片填充到状态栏 * * @param activity 需要设置的activity
     */
    public static void setTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }



    /**
     * 携带数据的页面跳转
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivity(Intent intent,Class<?> clz) {
        intent.setClass(this, clz);
        startActivity(intent);
    }
    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unRegister(this);
        AppManager.getAppManager().finishActivity(this);
    }


//    public View setEmpty(String string) {
//        View emptyview = View.inflate(this, R.layout.view_empty, null);
//        TextView textViewMessage;
//        Button btEmpty;
//        textViewMessage = emptyview.findViewById(R.id.textViewMessage);
//        btEmpty = emptyview.findViewById(R.id.bt_empty);
//        btEmpty.setVisibility(View.GONE);
//        textViewMessage.setText(string);
//        return emptyview;
//    }
}
